/**
 * 刮刮卡功能实现
 * 查看CCMask的源码
 */
cc.Class({
    extends: cc.Component,

    properties: {
        mask: cc.Mask,
    },

    onLoad: function () {
        //监听事件
        this.node.on(cc.Node.EventType.TOUCH_START, this._onTouchBegin, this);
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
        this.node.on(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancel, this);
    },

    onDestroy: function () {
        //销毁事件
        this.node.off(cc.Node.EventType.TOUCH_START, this._onTouchBegin, this);
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMoved, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
        this.node.off(cc.Node.EventType.TOUCH_CANCEL, this._onTouchCancel, this);
    },

    // start: function () {},

    _onTouchBegin: function (event) {
        cc.log('_onTouchBegin');
        this._getPointAndCircle(event);
    },

    _onTouchMoved: function (event) {
        cc.log('_onTouchMoved');
        this._getPointAndCircle(event);
    },

    _onTouchEnd: function (event) {
        cc.log('_onTouchEnd');
        this._getPointAndCircle(event);
    },

    _onTouchCancel: function (event) {
        cc.log('_onTouchCancel');
    },

    _getPointAndCircle: function (event) {
        //获取触摸点
        var point = event.touch.getLocation();
        //将一个点转换到节点 (局部) 空间坐标系，这个坐标系以锚点为原点。
        point = this.node.convertToNodeSpaceAR(point);
        this._addCircle(point);
    },

    _addCircle: function (point) {
        var stencil = this.mask._clippingStencil;
        //白色透明
        var color = cc.color(255, 255, 255, 0);
        stencil.drawPoly(this.mask._calculateCircle(point, cc.p(50, 50), 64), color, 0, color);
        if (!CC_JSB) {
            cc.renderer.childrenOrderDirty = true;
        }
    },
    toHome: function (event, data) {
        cc.director.loadScene("welcome");
    }
});
